package com.aiyige.utils.banner;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.aiyige.R;
import com.aiyige.arouter.Router;
import com.aiyige.base.eventbus.EventClickBannerItem;
import com.aiyige.utils.StringUtils;
import com.aiyige.utils.widget.RoundCornerImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class BannerFrag extends Fragment {
    BannerAdapter.Data data;
    @BindView(R.id.bannerIv)
    RoundCornerImageView bannerIv;
    Unbinder unbinder;

    public BannerFrag() {
        data = new BannerAdapter.Data();
    }

    public static BannerFrag newInstance(BannerAdapter.Data data) {
        BannerFrag fragment = new BannerFrag();
        fragment.setData(data);
        return fragment;
    }

    public void setData(BannerAdapter.Data data) {
        this.data = data;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.banner_frag, container, false);
        unbinder = ButterKnife.bind(this, view);
        Glide.with(this)
                .load(StringUtils.escapeEmptyString(data.getUrl()))
                .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg))
                .into(bannerIv);
        return view;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.bannerIv)
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bannerIv:
                Router.start(data.getRouter(),data.getId());
                break;
            default:
                break;
        }
    }
}
